package com.gaswell.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gaswell.entity.Qba01;
import com.gaswell.entity.Qba01M;
import com.gaswell.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Component    //1.主要用于标记配置类，兼备Component的效果。
@EnableAsync  // 2.开启定时任务
public class SaticScheduleTask {
    @Autowired
    IQba01Service qba01Service;
    @Autowired
    IQba01MService qba01MService;
    @Autowired
    IQfc01Service qfc01Service;
    @Autowired
    IQaa10Service qaa10Service;
    @Autowired
    QjService qjService;

    //3.添加定时任务    定时获取oracle中井日数据，并将数据处理后转移到mysql中
    @Async
    @Scheduled(cron = "0 0 6,12,18 * * ?")
//    @Scheduled(cron = "0 */1 * * * ?")
    public void configureTasks() {
        synchronized (ObjectUtils.instance) {
            Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
            calendar.add(Calendar.DAY_OF_MONTH, -1);
//        获取前一天的日期，防止有信息丢失
            Date date_1 = calendar.getTime();
            String startdate = DateUtils.dateToString(date_1);
            startdate += "  00:00:00";
            QueryWrapper<Qba01> qba01QueryWrapper = new QueryWrapper<>();
            System.out.println(DateUtils.stringTosqldate2(startdate));
            qba01QueryWrapper.ge("rq", DateUtils.stringTosqldate2(startdate));
            List<Qba01> list = qba01Service.list(qba01QueryWrapper);
            List<String> allSzhjh = qjService.findAllSzhjh();
            QueryWrapper<Qba01M> qba01QueryWrapper1 = new QueryWrapper<>();
            for (Qba01 qba01 : list) {
                if (allSzhjh.contains(qba01.getJh())) {
                    qba01QueryWrapper1.clear();
                    qba01QueryWrapper1.eq("jh", qba01.getJh())
                            .eq("rq", qba01.getRq());
                    List list1 = qba01MService.list(qba01QueryWrapper1);
//            查不到，说明信息还没进去
                    if (list1 == null || list1.size() == 0) {
//                    System.out.println("更新数据");
                        Qba01M qba01M = ObjectUtils.qba01ToQba01M(qba01);
                        List<String> jiYeAndDD = qba01MService.getJiYeAndDD(qba01);
                        qba01M.setJy(jiYeAndDD.get(0))
                                .setDd(jiYeAndDD.get(1));
                        qba01MService.save(qba01M);

                    }
                }
            }
        }


    }

    public static void main(String[] args) {
        LinkedList<String> strings = new LinkedList<>();
        LinkedList<String> strings2 = new LinkedList<>();
        strings.add("1");
        strings2.add("@");
        strings.addAll(strings2);
        System.out.println(strings);

    }
}